<?php
/**
 * 滚动图
 * @author Ice <iceinto@mallog.com.cn>
 */
class SlideAction extends LoginAction
{
	private $type = 'slide';

	public function index()
	{
		$malls = M('Market')->field('id,name')->select();
		$this->assign('malls', $malls);
		$this->display();
	}

	/**
	 * 商场首页
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	public function mall(){
		$mid = intval(getGet('mid'));
		$fid = intval(getGet('fid'));
		$SlideModel = M('Slide');
		//获取商场楼层信息
		$map = array();
		$map['mid'] = $mid;
		$floors = M('Floor')->where($map)->field('id,sain,name')->select();
		//统计宣传图数量
		unset($map);
		$map['mid'] = $mid;
		$map['type'] = 3;
		$count['p'] = $SlideModel->where($map)->count();
		unset($map);
		$map['mid'] = $mid;
		$map['type'] = 4;
		$count['a'] = $SlideModel->where($map)->count();
		if($mid && $fid){
			$brands = D('FloorBrand')->getFbAll($mid, $fid);
			//获取品牌下面活动滚动条数量
			foreach($brands as $key=>$b){
				unset($map);
				$map['mid'] = $mid;
				$map['fid'] = $fid;
				$map['bid'] = $b['bid'];
				$brands[$key]['count'] = $SlideModel->where($map)->count();
			}
			$this->assign('brands', $brands);
		}
		$this->assign('count', $count);
		$this->assign('mall', $this->getMall($mid));
		$this->assign('floors', $floors);
		$this->display();
	}

	/**
	 * 品牌列表
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	public function brands(){
		$BrandModel = M('Brand');
		$SlideModel = M('Slide');
		$map = array();
		$Page = $this->getPage($BrandModel->count());
		$brands = $BrandModel->field('id,name,initials')->limit($Page->firstRow.','.$Page->listRows)->order('initials ASC')->select();
		foreach($brands as $key=>$b){
			$map['bid'] = $b['id'];
			$map['type'] = 1;
			$brands[$key]['count'] = $SlideModel->where($map)->count();//统计宣传图数量
		}
		$this->assign('brands', $brands);
		$this->assign('page', $Page->show());
		$this->display();
	}

	/**
	 * 品牌宣传图列表
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	public function brand(){
		$bid = intval(getGet('bid'));
		if(!$bid){
			$this->error('必须传递品牌标识！');
		}
		$ads = D('Slide')->getPropagandas(0, $bid);
		$this->assign('ads', $ads);
		$this->assign('bid', $bid);
		$this->display();
	}

	/**
	 * 添加滚动图
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	public function add(){
		$type = intval(getGet('type'));
		$mid = intval(getGet('mid'));
		$fid = intval(getGet('fid'));
		$bid = intval(getGet('bid'));
		if($mid){
			$this->assign('mall', $this->getMall($mid));
		}
		if($fid){
			$this->assign('floor', $this->getFloor($mid, $fid));
		}
		if($bid){
			$this->assign('brand', $this->getBrand($bid));
		}
		//显示时间选项
		if($type == 2 || $type == 4){
			$this->assign('isShowTime', true);
		}
		if (isPost()) {
			$SlideModel = D('Slide');
			$data = $this->getPostData();
			$fId = $this->uploadFiles($this->type);//上传图片
			if ($fId) {
				$data['image'] = $fId;
			}
			$rs = false;
			$data = $SlideModel->create($data);
			if($data){
				$rs = $SlideModel->add();
				if ($rs) {
					$this->successMessage('添加成功！');
				}
			}
			if(!$rs){
				//添加失败删除上传图片
				$this->delUpLoadFile($fId, $this->type);
				$this->error('添加失败！');
			}
		}
		$this->display();
	}

	/**
	 * 修改滚动图
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	public function edit(){
		$id = intval(getGet('id'));
		$ad= M('Slide')->find($id);
		//显示时间选项
		if($ad['type'] == 2 || $ad['type'] == 4){
			$this->assign('isShowTime', true);
		}
		if(!$ad){
			$this->error('对应信息不存在！');
		}
		if(isPost()){
			$SlideModel = D('Slide');
			$isUpLoad = false;
			$image = 0;
			$fId = 0;
			$data['title'] = trim(getPost('title'));
			$data['mid'] = $ad['mid'];
			$data['fid'] = $ad['fid'];
			$data['bid'] = $ad['bid'];
			$data['id'] = $id;
			if($ad['type'] == 2 || $ad['type'] == 4){
				$data['start_time'] = strtotime(getPost('start_time'));
				$data['end_time'] = strtotime(getPost('end_time'));
				if ($data['start_time'] > $data['end_time']) {
					$this->error('活动结束时间不能小于开始时间！');
				}
			}
			$data = $SlideModel->create($data);
			if(!$data){
				$this->error('修改、数据不对！');
			}else{
				if ($_FILES['image']['name']) {
					$fId = $this->uploadFiles($this->type);
					if ($fId) {
						$image = $ad['image'];//交换id
						$data['image'] = $fId;
						$isUpLoad = true;
					}
				}
			}
			if($SlideModel->save($data)){
				if ($isUpLoad && $image) {
					//删除老的图片信息
					$this->delUpLoadFile($image, $this->type);
				}
				$ad= M('Slide')->find($id);
				$this->successMessage('修改成功！');
			}else{
				//删除修改失败的图片
				$this->delUpLoadFile($fId, $this->type);
				$this->error('修改失败！');
			}
		}
		$ad['image_url'] = $this->getManagedUrl($ad['image'],'slide');
		$this->assign('ad', $ad);
		$this->display();
	}

	/**
	 * 获取POST提交的数据
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	private function getPostData()
	{
		$data = array();
		$data['title'] = getPost('title');
		$data['mid'] = intval(getGetOrPost('mid'));
		$data['fid'] = intval(getGetOrPost('fid'));
		$data['bid'] = intval(getGetOrPost('bid'));
		$data['start_time'] = strtotime(getPost('start_time'));
		$data['end_time'] = strtotime(getPost('end_time'));
		if ($data['start_time'] > $data['end_time']) {
			$this->error('活动结束时间不能小于开始时间！');
		}
		//1品牌宣传 2品牌活动 3商场宣传 4商场活动
		if($data['mid'] && !$data['fid'] && !$data['bid']){
			//商场宣传图
			$data['type'] = 3;
		}
		if(!$data['mid'] && !$data['fid'] && $data['bid']){
			//品牌宣传
			$data['type'] = 1;
		}
		if($data['mid'] && !$data['fid'] && !$data['bid'] && $data['start_time'] && $data['end_time']){
			//商场活动
			$data['type'] = 4;
		}
		if($data['mid'] && $data['fid'] && $data['bid'] && $data['start_time'] && $data['end_time']){
			//品牌活动
			$data['type'] = 2;
		}
		//判断是否需要时间
		if($data['type']!=4 && $data['type']!=2){
			unset($data['start_time']);
			unset($data['end_time']);
		}
		return $data;
	}

	/**
	 * 商场宣传和活动列表显示
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	public function ms(){
		$mid = intval(getGetOrPost('mid'));
		$type = intval(getGetOrPost('type'));
		$SlideModel = D('Slide');
		$map = array();
		$ads = array();
		if($type == 3){
			$ads = $SlideModel->getPropagandas($mid, 0);
		}else if($type == 4){
			$ads = $SlideModel->getActivites($mid, 0, 0);
		}
		$this->assign('ads', $ads);
		$this->assign('mall', $this->getMall($mid));
		$this->display();
	}

	/**
	 * 品牌活动列表显示
	 * @author Ice <iceinto@mallog.com.cn>
	 */
	public function bs(){
		$mid = intval(getGetOrPost('mid'));
		$fid = intval(getGetOrPost('fid'));
		$bid = intval(getGetOrPost('bid'));
		
		if($mid && $fid && $bid){
			$SlideModel = D('Slide');
			$ads = $SlideModel->getActivites($mid, $fid, $bid);
			$this->assign('ads', $ads);
		}else{
			$this->error('品牌活动信息获取，提供数据错误！');
		}
		$this->display();
	}
}
?>
